package org.rufolfheszele.andpixelbyandpixel;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;

class ConnectionTask implements Runnable
{
	public ConnectionTask(Socket clientSocket)
	{
		mClientSocket = clientSocket;
	}

	@Override
	public void run()
	{
		MainGameController gameController = (MainGameController)MainApplication.getMainApplication().getMainActivity().getMainImageView().getGameController();
		
		gameController.handleSocketEvent(MainGameController.SocketEventType.SOCKET_CONNECTION, mClientSocket);
	}
	
	private Socket mClientSocket = null;
}

public class ListenerThread extends Thread
{
	public ListenerThread(Activity activity)
	{
		mActivity = activity;
		this.start();
	}
	
	@Override
	public void run()
	{
		try
		{
			Socket clientSocket = null;
			int portNumber = MainApplication.getMainApplication().getConfiguration().getNetworkConfiguration().getPortNumber();
			ServerSocket serverSocket = new ServerSocket(portNumber);
			
			while(true)
			{
				clientSocket = serverSocket.accept();
				mActivity.runOnUiThread(new ConnectionTask(clientSocket));
			}
			
		}
		catch (IOException exception)
		{
			
		}
	}
	
	private Activity mActivity = null;
}
